package gameadminlogic

import (
	"context"
	"github.com/jinzhu/copier"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/game"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/game/internal/svc"
	"navi-mumbai/app/rpc/game/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryExchangeGameMoneyConfigLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryExchangeGameMoneyConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryExchangeGameMoneyConfigLogic {
	return &QueryExchangeGameMoneyConfigLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查询游戏兑换比例
func (l *QueryExchangeGameMoneyConfigLogic) QueryExchangeGameMoneyConfig(in *pb.QueryExchangeGameMoneyConfigReq) (*pb.QueryExchangeGameMoneyConfigResp, error) {
	defer utils.RecoverPanic()

	var list []*game.ExchangeGameMoneyConfig
	var err error
	list, err = l.svcCtx.ExchangeGameMoneyConfigModel.Find(l.ctx, in.PageNo, in.PageSize)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	var resp pb.QueryExchangeGameMoneyConfigResp
	resp.List = make([]*pb.ExchangeGameMoneyConfig, 0)
	if len(list) == 0 {
		return &resp, nil
	}

	// 获取数量
	if in.PageNo <= 1 {
		resp.Total, err = l.svcCtx.ExchangeGameMoneyConfigModel.Count(l.ctx)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return &resp, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
	}

	//
	for i := 0; i < len(list); i++ {
		var vl pb.ExchangeGameMoneyConfig
		_ = copier.Copy(&vl, list[i])
		resp.List = append(resp.List, &vl)
	}

	return &resp, nil
}
